﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using YJKDataBase;

namespace YJKArchiFormat
{
	public partial class DlgAddCategory : System.Windows.Forms.Form
	{
		public DlgAddCategory(IDlgBase main)
		{
			this.mForm = main;
			this.InitializeComponent();
			if (this.mForm.TrCategory.SelectedNode != null && this.mForm.TrCategory.SelectedNode.Tag != null)
			{
				this.cbxSubject.Items.Add(this.mForm.CurrentSubject);
				this.cbxSubject.SelectedIndex = 0;
				this.cbxCategory.Items.Add(this.mForm.TrCategory.SelectedNode.Tag);
				this.cbxCategory.SelectedIndex = 0;
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			if (this.cbxCategory.SelectedItem is Parent)
			{
				try
				{
					Parent parent = (Parent)this.cbxCategory.SelectedItem;
					DbCategory dbCategory = new DbCategory(this.tbxCName.Text);
					dbCategory.Version = this.tbxVersion.Text;
					dbCategory.Description = this.rtbDescription.Text;
					dbCategory.Image = new Bitmap(200, 200);
					if (parent.AddCategory(dbCategory))
					{
						TreeNode treeNode = new TreeNode(dbCategory.Name);
						treeNode.ImageIndex = 1;
						treeNode.Tag = dbCategory;
						treeNode.Name = dbCategory.Name;
						this.mForm.TrCategory.SelectedNode.Nodes.Add(treeNode);
						this.tbxCName.Text = "";
					}
				}
				catch (Exception ex)
				{
					MessageBox.Show(ex.Message);
				}
			}
			if (!this.chkContinue.Checked)
			{
				base.Close();
			}
		}

		public static DlgAddCategory GetInstance(IDlgBase main)
		{
			if (DlgAddCategory.dac == null || DlgAddCategory.dac.IsDisposed)
			{
				DlgAddCategory.dac = new DlgAddCategory(main);
			}
			return DlgAddCategory.dac;
		}

		private void tbxCName_TextChanged(object sender, EventArgs e)
		{
			Regex regex = new Regex("^[0-9a-zA-Z一-龥]+$");
			if (this.tbxCName.Text.Trim() == "" || !regex.IsMatch(this.tbxCName.Text.Trim()))
			{
				this.btnOK.Enabled = false;
				return;
			}
			this.btnOK.Enabled = true;
		}

		private void cbxSubject_SelectedIndexChanged(object sender, EventArgs e)
		{
		}

		private void button1_Click(object sender, EventArgs e)
		{
		}

		private void tbxParamName_TextChanged(object sender, EventArgs e)
		{
		}

		private void lbxRecordParam_SelectedIndexChanged(object sender, EventArgs e)
		{
		}

		private void btnDel_Click(object sender, EventArgs e)
		{
		}

		private void DlgAddCategory_Load(object sender, EventArgs e)
		{
		}

		private void lbxRecordParam_DoubleClick(object sender, EventArgs e)
		{
		}

		private void DlgAddCategory_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		private static DlgAddCategory dac;

		private IDlgBase mForm;
	}
}
